<?php

namespace app\result;

use app\model\UpdateLog;

class UpdateLogResult extends BaseResult
{
    //更新日志
    public function list($args = [])
    {
        $page = intval($args['page'] ?? 1);
        $pageSize = intval($args['limit'] ?? 10);
        $where = [];
        if (!empty($args['keyword'])) {
            $where[] = ['content', 'like', '%' . trim($args['keyword']) . '%'];
        }
        $model = new UpdateLog();
        $total = $model->data_count($where);
        $data = $model->data_list($where, '*', 'up_time desc', $page, $pageSize);
        if ($data) {
            foreach ($data as &$v) {
                $v['content'] = str_replace(["\r\n", "\n", "\r"], "<br>", $v['content']);
            }
            unset($v);
        }
        return $this->success_return('success', [
            'total' => $total,
            'list' => $data
        ]);
    }

    public function get($args = [])
    {
        $id = intval($args['id'] ?? 0);
        $model = new UpdateLog();
        $data = $model->data_get(['id' => $id]);
        return $this->success_return('success', $data);
    }

    public function save_data($args): array
    {
        $rules = [
            ['content', 'empty|max:500|min:5', '内容不能为空|内容长度不能大于500|内容长度不能小于5个字'],
            ['up_time', 'empty', '请选择更新时间']
        ];
        $check = check_param($args, $rules);
        if ($check['status'] != 0) {
            return self::error_return($check['msg']);
        }
        $id = intval($args['id'] ?? 0);
        $baseData = [
            'content' => $args['content'],
            'up_time' => date('Y-m-d', strtotime($args['up_time'])),
        ];
        $model = new UpdateLog();
        if ($id) {
            $res = $model->data_update(['id' => $id], $baseData);
        } else {
            $res = $model->data_add($baseData);
            if (!$res) {
                return $this->error_return('添加失败');
            }
        }
        return $this->success_return('success');
    }

    public function del_data($args): array
    {
        $id = intval($args['id'] ?? 0);
        $model = new UpdateLog();
        $res = $model->data_del(['id' => $id]);
        return $this->success_return('success');
    }
}
